#!/bin/bash

echo -e "
==============================================================================
          _       ___     ___     _     _            _  __  ___   ___
         | |     / _ \   / __|   /_\   | |     ___  | |/ / ( _ ) / __|
         | |__  | (_) | | (__   / _ \  | |__  |___| | ' <  / _ \ \__ \\
         |____|  \___/   \___| /_/ \_\ |____|       |_|\_\ \___/ |___/
==============================================================================
"
echo "**local-k8s** (Windows + VMware + Centos + Kubernetes + Docker) windwos本机实现k8s 集群工具"
echo "**gitee** https://gitee.com/dev-linhu/local-k8s"

echo "=======================================  禁用防火墙 ====================="
systemctl stop firewalld && systemctl disable firewalld

echo "========================= 安装 git、vim、wget、containerd ==============="

yum -y install git vim wget containerd
echo "------ 配置并重启 containerd "
systemctl start containerd
cat > /etc/containerd/config.toml << EOF


#disabled_plugins = ["cri"]

#root = "/var/lib/containerd"
#state = "/run/containerd"
#subreaper = true
#oom_score = 0

#[grpc]
#  address = "/run/containerd/containerd.sock"
#  uid = 0
#  gid = 0

#[debug]
#  address = "/run/containerd/debug.sock"
#  uid = 0
#  gid = 0
#  level = "info"
EOF
systemctl restart containerd

echo "======================================= 关闭swap ========================="
swapoff -a

echo "======================================= 配置节点名称，以及hosts文件 ===="
echo "------ 是否是主节点 (y/n):"
read name
echo "------ 请输入主节点ip:"
read master_ip


echo "======================================= 安装 docker-ce-18.06 ==================="
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo && yum -y install docker-ce-18.06.1.ce-3.el7

echo "------ 设启动docker"
systemctl enable docker && systemctl start docker

echo "------ 设置docker配置文件"
cat > /etc/docker/daemon.json << EOF
{
  	"registry-mirrors": ["https://nz3z0zzn.mirror.aliyuncs.com"],
	"insecure-registries": ["$master_ip:5000"],
	"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF

echo "------ 预处理解决 kubectl init 启动错误"
systemctl daemon-reload && systemctl restart docker && echo "1" >  /proc/sys/net/bridge/bridge-nf-call-iptables

if [ $name == "y" ]
then
hostnamectl set-hostname master
echo "------ master节点创建私有镜像库 {ip}:5000 "
docker pull registry && docker run -itd -p 5000:5000 --restart always -v /opt/data/registry/:/var/lib/registry --name registry registry 	
fi

echo "======================================= 安装 kubernetes、kubeadm、kubectl ================"

echo "------ 配置添加阿里云YUM软件源"
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
echo "------ 安装kubernetes、kubeadm、kubectl 设置kubelet 开机启动"
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0 && systemctl enable kubelet

echo "------ kubeadm reset 重启docker服务"
systemctl stop docker
kubeadm reset -f
systemctl start docker


if [ $name == "y" ]
then
echo "======================================= registry.k8s.io 镜像拉取 推送至本地镜像库 错误信息: Initial timeout of 40s passed. ================"
echo "------当前只针对 1.28.2版本生效 请自行更改 、  "

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.28.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.28.2 registry.k8s.io/kube-apiserver:v1.28.2

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.28.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.28.2 registry.k8s.io/kube-controller-manager:v1.28.2

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.28.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.28.2 registry.k8s.io/kube-scheduler:v1.28.2

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.28.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.28.2 registry.k8s.io/kube-proxy:v1.28.2

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9 registry.k8s.io/pause:3.9

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.9-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.9-0 registry.k8s.io/etcd:3.5.9-0

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.10.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.10.1 registry.k8s.io/coredns/coredns:v1.10.1

echo "======================================= master 节点初始化 ================"
kubeadm init \
  --apiserver-advertise-address=$master_ip \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.18.0 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16
echo "------  请保存 kubeadm join ... 子节点运行 "

else

echo "======================================= node 节点初始化 ================"
kubeadm reset -f
echo "------  请复制 /etc/kubernetes/admin.conf 到子节点后点击回车进行下一步:"
read next

export KUBECONFIG=/etc/kubernetes/admin.conf
kubectl get node

fi
mkdir -p $HOME/.kube && sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config &&  sudo chown $(id -u):$(id -g) $HOME/.kube/config

echo "======================================= 安装 cni 网络插件 ================"
kubectl apply -f https://gitee.com/dev-linhu/local-k8s/raw/master/yaml/kube-flannel.yml
kubectl get pods -n kube-system

echo "======================================= 安装完毕 ================"
exit
